//
//  AblityViewController.m
//  penzer
//
//  Created by caiyc on 2018/10/11.
//  Copyright © 2018年 caiyc. All rights reserved.
//

#import "AblityViewController.h"
#import "HostTag.h"
@interface AblityViewController ()<tagdelegate>
@property(nonatomic,strong)NSMutableArray *dataSouce;
@property(nonatomic,strong)NSMutableArray *selectTag;
@property(nonatomic,strong)UIView *danmuTextView;//s发弹幕的弹出视图
@property(nonatomic,strong)UITextField *sendDanmutext;
@property(nonatomic,strong)NSMutableArray *addBlityArr;
@end

@implementation AblityViewController
-(NSMutableArray *)addBlityArr{
    if(!_addBlityArr){
        _addBlityArr = [NSMutableArray array];
    }
    return _addBlityArr;
}

-(NSMutableArray *)selectTag{
    if(!_selectTag){
        _selectTag = [NSMutableArray array];
    }
    return _selectTag;
}
-(NSMutableArray *)dataSouce{
    if(!_dataSouce){
        _dataSouce = [NSMutableArray array];
    }
    return _dataSouce;
}
- (void)viewDidLoad {
    [super viewDidLoad];
     [self setNavigation:1 andTitle:@"选择标签" andNaviColor:[UIColor whiteColor] andTitleColor:nil];
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillShow:) name:UIKeyboardWillShowNotification object:nil];
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillBeHiden:) name:UIKeyboardWillHideNotification object:nil];
    [self loadData];
    // Do any additional setup after loading the view from its nib.
}
-(void)loadtagView{
    NSMutableArray *lab = [NSMutableArray array];
    for(int i =0;i<self.dataSouce.count;i++){
        [lab addObject:self.dataSouce[i][@"name"]];
    }
    HostTag *tag = [[HostTag alloc]initWithFrame:CGRectMake(0, 0, screen_width, 0)];
    [tag setArr:lab];
    tag.delegate = self;
    [self.tagViews addSubview:tag];
}
-(void)handleSelectTag:(NSString *)keyWord andTag:(NSInteger)index AndPindex:(NSInteger)pindex Andselct:(BOOL)select{
    if(select==1){
        //选中
    if(self.selectTag.count==3){
        [WToast showWithText:@"最多选择三个标签"];
    }else{
        [self.selectTag addObject:self.dataSouce[index]];
    }
    }else{
        //取消选中
        NSDictionary *dic = self.dataSouce[index];
        NSInteger ids = [dic[@"id"]integerValue];
        for(int i =0;i<self.selectTag.count;i++){
            if(ids==[self.selectTag[i][@"id"]integerValue]){
                [self.selectTag removeObjectAtIndex:i];
            }
        }
    }
}
-(void)loadData{
    NSDictionary *param = @{@"ac":@"member",
                            @"op":@"member",
                            @"ctrl":@"live_anchor",
                            @"ta":@"tags"};
    [self weqPostHandle:param success:^(id response) {
        if(response){
            [self.dataSouce addObjectsFromArray:response[@"data"]];
            [self loadtagView];
        }
    } failure:^(NSError *error) {
        
    }];
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/
-(void)keyboardWillBeHiden:(NSNotification *)aNotification{
    self.danmuTextView.frame = CGRectMake(0, 10000, screen_width, 50);
}
#pragma mark 键盘弹出的响应事件

- (void)keyboardWillShow:(NSNotification *)aNotification {
    
    /* 获取键盘的高度 */
    NSDictionary *userInfo = aNotification.userInfo;
    NSValue *aValue = [userInfo objectForKey:UIKeyboardFrameEndUserInfoKey];
    CGRect keyboardRect = aValue.CGRectValue;
    /* 输入框上移 */
    CGFloat padding = 20;
    [UIView beginAnimations:@"ResizeForKeyboard" context:nil];
    [UIView setAnimationDuration:0.5f];
    //将视图的Y坐标向上移动offset个单位，以使下面腾出地方用于软键盘的显示
    self.danmuTextView.frame = CGRectMake(0,screen_height-50-keyboardRect.size.height ,screen_width, 50);
    [UIView commitAnimations];
    
}
-(UIView *)danmuTextView{
    if(!_danmuTextView){
        _danmuTextView = [[UIView alloc]initWithFrame:CGRectMake(0, 1000, screen_width, 50)];
        _danmuTextView.backgroundColor = RGB(225, 225, 225);
        //        _danmuTextView.layer.cornerRadius = 25;
        //        _danmuTextView.layer.masksToBounds = 1;
        [_danmuTextView addSubview:self.sendDanmutext];
        UIButton *send_Btn = [UIButton buttonWithType:UIButtonTypeCustom];
        send_Btn.frame = CGRectMake(screen_width-60, 0, 60, 50);
        [send_Btn setTitle:@"添加" forState:UIControlStateNormal];
        [send_Btn setTitleColor:[UIColor grayColor] forState:UIControlStateNormal];
        send_Btn.titleLabel.font = [UIFont systemFontOfSize:15];
        [_danmuTextView addSubview:send_Btn];
        [send_Btn addTarget:self action:@selector(addAblity) forControlEvents:UIControlEventTouchUpInside];
        [self.view addSubview:_danmuTextView];
    }
    return _danmuTextView;
}
-(UITextField *)sendDanmutext{
    if(!_sendDanmutext){
        _sendDanmutext = [[UITextField alloc]initWithFrame:CGRectMake(10, 5, screen_width-70, 40)];
        _sendDanmutext.placeholder = @"添加标签";
        _sendDanmutext.layer.cornerRadius = 20;
        _sendDanmutext.layer.masksToBounds = 1;
        _sendDanmutext.leftView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, 10, 0)];   //设置显示模式为永远显示(默认不显示)
        _sendDanmutext.leftViewMode = UITextFieldViewModeAlways;
        
        _sendDanmutext.backgroundColor = [UIColor whiteColor];
        
    }
    return _sendDanmutext;
}
- (IBAction)add:(id)sender {
    [self danmuTextView];
    [self.sendDanmutext becomeFirstResponder];
   // [self.danmuTextView becomeFirstResponder];
   // [self.sendDanmutext becomeFirstResponder];
}
-(void)addAblity{
    [self.view endEditing:1];
    [self addTosevie];
    [self.addBlityArr addObject:self.sendDanmutext.text];
    for(UIView *view in [self.addTagview subviews]){
        [view removeFromSuperview];
    }
    HostTag *tag = [[HostTag alloc]initWithFrame:CGRectMake(0, 0, screen_width, 0)];
    [tag setArr:self.addBlityArr];
    tag.delegate = self;
    [self.addTagview addSubview:tag];
}
-(void)addTosevie{
    NSDictionary *param = @{@"ac":@"member",
                            @"op":@"member",
                            @"ctrl":@"live_anchor",
                            @"ta":@"add_usertags",@"name":self.sendDanmutext.text};
    [self weqPostHandle:param success:^(id response) {
        
    } failure:^(NSError *error) {
        
    }];
}
@end
